package com.xuetang.controller.admin;

import com.xuetang.entity.CommunityComment;
import com.xuetang.entity.CommunityPost;
import com.xuetang.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员社区管理控制器
 */
@RestController
@RequestMapping("/admin/api/community")
public class AdminCommunityController {

    @Autowired
    private CommunityService communityService;

    /**
     * 获取最近的帖子列表
     */
    @GetMapping("/posts/recent")
    public Map<String, Object> getRecentPosts(@RequestParam(defaultValue = "10") int limit) {
        Map<String, Object> result = new HashMap<>();

        try {
            List<CommunityPost> posts = communityService.getRecentPosts(limit);

            result.put("code", 0);
            result.put("message", "获取成功");
            result.put("data", posts);
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }

        return result;
    }

    /**
     * 获取最近的评论列表
     */
    @GetMapping("/comments/recent")
    public Map<String, Object> getRecentComments(@RequestParam(defaultValue = "10") int limit) {
        Map<String, Object> result = new HashMap<>();

        try {
            List<CommunityComment> comments = communityService.getRecentComments(limit);

            result.put("code", 0);
            result.put("message", "获取成功");
            result.put("data", comments);
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }

        return result;
    }

    /**
     * 获取帖子详情
     */
    @GetMapping("/posts/{id}")
    public Map<String, Object> getPostDetail(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();

        try {
            CommunityPost post = communityService.getPostDetail(id, null);

            if (post != null) {
                result.put("code", 0);
                result.put("message", "获取成功");
                result.put("data", post);
            } else {
                result.put("code", 404);
                result.put("message", "帖子不存在");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }

        return result;
    }

    /**
     * 删除帖子
     */
    @DeleteMapping("/posts/{id}")
    public Map<String, Object> deletePost(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();

        try {
            boolean success = communityService.deletePost(id);

            if (success) {
                result.put("code", 0);
                result.put("message", "删除成功");
            } else {
                result.put("code", 1);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "删除失败：" + e.getMessage());
        }

        return result;
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/comments/{id}")
    public Map<String, Object> deleteComment(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();

        try {
            boolean success = communityService.deleteComment(id);

            if (success) {
                result.put("code", 0);
                result.put("message", "删除成功");
            } else {
                result.put("code", 1);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "删除失败：" + e.getMessage());
        }

        return result;
    }

    /**
     * 更新帖子状态
     */
    @PutMapping("/posts/{id}/status")
    public Map<String, Object> updatePostStatus(
            @PathVariable Long id,
            @RequestBody Map<String, Integer> body) {
        Map<String, Object> result = new HashMap<>();

        try {
            Integer status = body.get("status");

            if (status == null) {
                result.put("code", 400);
                result.put("message", "参数错误，缺少status字段");
                return result;
            }

            CommunityPost post = communityService.getPostDetail(id, null);

            if (post == null) {
                result.put("code", 404);
                result.put("message", "帖子不存在");
                return result;
            }

            post.setStatus(status);
            boolean success = communityService.updatePost(post);

            if (success) {
                result.put("code", 0);
                result.put("message", "更新成功");
            } else {
                result.put("code", 1);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "更新失败：" + e.getMessage());
        }

        return result;
    }

    /**
     * 更新评论状态
     */
    @PutMapping("/comments/{id}/status")
    public Map<String, Object> updateCommentStatus(
            @PathVariable Long id,
            @RequestBody Map<String, Integer> body) {
        Map<String, Object> result = new HashMap<>();

        try {
            Integer status = body.get("status");

            if (status == null) {
                result.put("code", 400);
                result.put("message", "参数错误，缺少status字段");
                return result;
            }

            CommunityComment comment = communityService.getCommentById(id);

            if (comment == null) {
                result.put("code", 404);
                result.put("message", "评论不存在");
                return result;
            }

            comment.setStatus(status);
            boolean success = communityService.updateComment(comment);

            if (success) {
                result.put("code", 0);
                result.put("message", "更新成功");
            } else {
                result.put("code", 1);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "更新失败：" + e.getMessage());
        }

        return result;
    }
}